﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;

namespace RN.ERP.Web.Material.Material
{
    public partial class MaterialStore : System.Web.UI.Page
    {
        /// <summary>
        /// 1.入库；0出库
        /// </summary>
        public int Status
        {
            get;
            set;
        }

        private DataTable _data
        {
            get
            {
                DataTable t = (DataTable)ViewState["table"];
                if (t == null)
                {
                    InitTable();
                    t = (DataTable)ViewState["table"];
                }
                return t;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gvMaterial.DataSource = _data;
                gvMaterial.DataBind();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable table = _data;
            DataRow newRow = table.NewRow();
            newRow["Index"] = gvMaterial.Rows.Count + 1;
            table.Rows.Add(newRow);
            gvMaterial.EditIndex = gvMaterial.Rows.Count;
            gvMaterial.DataSource = table;
            gvMaterial.DataBind();
        }

        private void InitTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add(new DataColumn("Index"));
            table.Columns.Add(new DataColumn("MName"));
            table.Columns.Add(new DataColumn("CName"));
            table.Columns.Add(new DataColumn("MCode"));
            table.Columns.Add(new DataColumn("CCode"));
            table.Columns.Add(new DataColumn("Num"));
            table.Columns.Add(new DataColumn("Price"));
            ViewState["table"] = table;
        }

        protected void gvMaterial_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvMaterial.EditIndex = e.NewEditIndex;
            DataTable table = _data;
            gvMaterial.DataSource = table;
            gvMaterial.DataBind();
        }

        protected void gvMaterial_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string index = ((Label)gvMaterial.Rows[e.RowIndex].Cells[0].FindControl("Label1")).Text.Trim();
            for (int i = _data.Rows.Count - 1; i >= 0; i--)
            {
                int i1 = 0;
                int i2 = 0;
                int.TryParse(_data.Rows[i]["Index"].ToString().Trim(), out i1);
                int.TryParse(index, out i2);
                if (i1 == i2)
                {
                    _data.Rows.RemoveAt(i);
                }
                else if (i1 > i2)
                {
                    _data.Rows[i]["index"] = i1 - 1;
                }
            }
            gvMaterial.DataSource = _data;
            gvMaterial.DataBind();
        }

        protected void gvMaterial_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvMaterial.EditIndex = -1;
            DataTable table = _data;
            gvMaterial.DataSource = table;
            gvMaterial.DataBind();
        }

        protected void gvMaterial_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            DataTable table = _data;
            string index, mName, cName, mCode = string.Empty, cCode = string.Empty, num = string.Empty, price = string.Empty;
            index = ((Label)gvMaterial.Rows[e.RowIndex].Cells[0].FindControl("Label1")).Text.Trim();
            bool isFound = false;
            foreach (DataRow r in table.Rows)
            {
                if (r["Index"].ToString().Trim() == index)
                {
                    mName = ((TextBox)gvMaterial.Rows[e.RowIndex].Cells[1].FindControl("Label2")).Text.Trim();
                    cName = ((TextBox)gvMaterial.Rows[e.RowIndex].Cells[2].FindControl("Label3")).Text.Trim();
                    mCode = ((HtmlInputHidden)gvMaterial.Rows[e.RowIndex].Cells[0].FindControl("hdnMCode")).Value.Trim();
                    cCode = ((HtmlInputHidden)gvMaterial.Rows[e.RowIndex].Cells[0].FindControl("hdnCCode")).Value.Trim();
                    num = ((TextBox)gvMaterial.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim();
                    price = ((TextBox)gvMaterial.Rows[e.RowIndex].Cells[5].Controls[0]).Text.Trim();
                    r["MName"] = mName;
                    r["CName"] = cName;
                    r["MCode"] = mCode;
                    r["CCode"] = cCode;
                    r["Num"] = num;
                    r["Price"] = price;
                    isFound = true;
                }
            }
            if (!isFound)
            {
                DataRow newRow = table.NewRow();
                mName = ((TextBox)gvMaterial.Rows[e.RowIndex].Cells[1].FindControl("Label2")).Text.Trim();
                cName = ((TextBox)gvMaterial.Rows[e.RowIndex].Cells[2].FindControl("Label3")).Text.Trim();
                mCode = ((HtmlInputHidden)gvMaterial.Rows[e.RowIndex].Cells[0].FindControl("hdnMCode")).Value.Trim();
                cCode = ((HtmlInputHidden)gvMaterial.Rows[e.RowIndex].Cells[0].FindControl("hdnCCode")).Value.Trim();
                num = ((TextBox)gvMaterial.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim();
                price = ((TextBox)gvMaterial.Rows[e.RowIndex].Cells[5].Controls[0]).Text.Trim();
                newRow["Index"] = index;
                newRow["MName"] = mName;
                newRow["CName"] = cName;
                newRow["MCode"] = mCode;
                newRow["CCode"] = cCode;
                newRow["Num"] = num;
                newRow["Price"] = price;
                table.Rows.Add(newRow);
            }
            string result = ValidateMate(mCode, cCode, num, price);
            if (string.IsNullOrEmpty(result))
            {
                gvMaterial.EditIndex = -1;
                gvMaterial.DataSource = table;
                gvMaterial.DataBind();
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), string.Empty, "alert('" + result + "');", true);
            }
        }

        private string ValidateMate(string mCode, string cCode, string num, string price)
        {
            string result = string.Empty;

            if (string.IsNullOrEmpty(mCode))
            {
                result = "请选择物资！";
            }
            int i = 0;
            if (int.TryParse(num, out i))
            {
                result = "数量必须为数字！";
            }
            double i2 = 0;
            if (double.TryParse(price, out i2))
            {
                result = "单价必须为货币型！";
            }

            return result;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

        }
    }
}